<template>
  <div class="app-container">
    <div class="search-container">
      <div class="search-wrap">
        <div style="display:inline-block;">
          <label class="radio-label" style="padding-left:0;">真实主机名/IP: </label>
          <el-input v-model="fieldName" placeholder="" style="width:240px;"/>
        </div>

        <div style="display:inline-block;">
          <label class="radio-label">镜像主机/IP: </label>
          <el-input v-model="fieldName" placeholder="" style="width:240px;"/>
        </div>
        <el-button :loading="downloadLoading" style="margin:0 0 20px 20px;" type="primary" icon="el-icon-search">
          搜索
        </el-button>
        <el-button :loading="downloadLoading" style="margin:0 0 20px 20px;" type="info" icon="el-icon-refresh-right">
          重置
        </el-button>
      </div>

      <div class="btn-wrap radio-label">
        <el-button :loading="downloadLoading" style="margin:0 0 20px 20px;" type="primary" icon="el-icon-circle-plus-outline"
        @click="dialogTableVisible = true">
          新增
        </el-button>
      </div>
    </div>

    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading..." :span-method="objectSpanMethod"
      border fit highlight-current-row>
      <el-table-column align="center" label="编号" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="真实主机名">
        <template slot-scope="scope">
          {{ scope.row.realName }}
        </template>
      </el-table-column>
      <el-table-column label="真实IP">
        <template slot-scope="scope">
          {{ scope.row.realIp }}
        </template>
      </el-table-column>
      <el-table-column label="镜像主机名">
        <template slot-scope="scope">
          {{ scope.row.virtualName }}
        </template>
      </el-table-column>
      <el-table-column label="镜像IP">
        <template slot-scope="scope">
          {{ scope.row.virtualIP }}
        </template>
      </el-table-column>
      <el-table-column label="访问关系">
        <template slot-scope="scope">
          {{ scope.row.link }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="260" align="center">
        <template slot-scope="{row}">
          <el-button
            type="warning"
            size="mini"
            icon="el-icon-edit"
          >
            修改
          </el-button>

          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
          >
            删除
          </el-button>
        </template>
        
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="1000">
      </el-pagination>
    </div>


    <el-dialog v-el-drag-dialog :visible.sync="dialogTableVisible" title="新增伪装主机" @dragDialog="handleDrag" width="1200px">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="120px">
        <el-row>
          <el-col :span="24">
            <div class="form-title">真实主机</div>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="真实主机" prop="title">
              <el-select v-model="temp.host" class="filter-item" placeholder="请选择主机" clearable filterable allow-create>
                <el-option v-for="item in hostOptions" :key="item" :label="item" :value="item" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="访问关系" prop="title">
              <el-select v-model="temp.type" class="filter-item" size="medium" placeholder="请选择" clearable>
                <el-option v-for="item in typeOptions" :key="item" :label="item" :value="item" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <div class="form-title">镜像主机</div>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="主机名称" prop="title">
              <el-input value="PC-S5"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机IP" prop="title">
              <el-input value="172.16.1.3"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogTableVisible = false">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList } from '@/api/article'
import { parseTime } from '@/utils'

import Pagination from '@/components/Pagination'
import elDragDialog from '@/directive/el-drag-dialog'

export default {
  name: 'Host',
  components: { Pagination },
  directives: { elDragDialog },
  data() {
    return {
      list: null,
      fieldName: '',
      listLoading: true,
      downloadLoading: false,
      autoWidth: true,

      formList: [
        {
          ip: '192.168.2.118,192.168.2.118,192.168.2.118,192.168.2.118,192.168.2.118...',
          server: 'SSH蜜罐,FTP蜜罐,MAIL蜜罐,SMB蜜罐...',
          status: '运行中'
        },
        {
          ip: '203.45.12.85,225,234.142.118,18.10.2.88...',
          server: '天融信上网行为管理系统蜜罐,天融信防火墙蜜罐,深信服WAF系统蜜罐,奇安信防火墙系统蜜罐...',
          status: '运行中'
        },
      ],
      dialogTableVisible: false,

      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published',
      },
      hostOptions: ['28.12.125.74','47.45.109.68','12.16.84.57','176.86.94.57','175.116.180.77','172.16.1.13','182.16.10.8','2.16.105.57','182.161.122.77'],
      typeOptions: ['外到内访问','内到外访问'],
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    flitterData(arr) {
      let spanOneArr = []
      let concatOne = 0
      arr.forEach((item, index) => {
          if (index === 0) {
              spanOneArr.push(1)
          } else {
              //realName 修改
              if (item.realName === arr[index - 1].v) {
                  //第一列需合并相同内容的判断条件
                  spanOneArr[concatOne] += 1
                  spanOneArr.push(0)
              }else {
                  spanOneArr.push(1)
                  concatOne = index
              }
          }
      })
      console.log("1",{
          one: spanOneArr,
      })
      return {
          one: spanOneArr,
      }
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const _row = this.flitterData(this.list).one[rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col,
        }
      }
      
    },
    fetchData() {
      this.listLoading = true
      let data = []
      fetchList().then(response => {
        data.push({
          realName: "PC-S1",
          realIp: "172.16.1.13",
          virtualName: "PC-S2",
          virtualIP: "172.16.1.2",
          link: "外到内"
        })
        data.push({
          realName: "PC-S1",
          realIp: "172.16.1.13",
          virtualName: "PC-S3",
          virtualIP: "172.16.1.3",
          link: "外到内"
        })
        data.push({
          realName: "PC-S1",
          realIp: "172.16.1.13",
          virtualName: "PC-S4",
          virtualIP: "172.16.1.3",
          link: "外到内"
        })
        data.push({
          realName: "Serv-DB",
          realIp: "172.16.2.10",
          virtualName: "Serv-DB1",
          virtualIP: "10.10.10.10",
          link: "内到外"
        })
        this.list = data
        this.listLoading = false
      })
    },
    handleDrag() {
      console.log("添加页面")
    }
  }
}
</script>

<style scoped>
.radio-label {
  font-size: 14px;
  color: #606266;
  line-height: 40px;
  padding: 0 12px 0 30px;
}
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: right;
}
.search-container {
  display: flex;
  justify-content: space-between;
}
.search-wrap {
  flex: 1;
}


.form-title {
  margin-bottom: 10px;
  padding-bottom: 10px;
  display: flex;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
  border-bottom: 1px solid #F1F1F1;
  box-sizing: border-box;
}
.el-select {
  width: 100%;
}
</style>
